package com.wfy.common.interceptor;

import com.wfy.common.util.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.NamedThreadLocal;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created with IntelliJ IDEA.
 * User: wangxindong
 * Date: 2017/9/30
 * Time: 20:20
 */
public class LogInterceptor implements HandlerInterceptor {

    // ================================================================
    // Constants
    // ================================================================

    private static final Logger LOGGER = LoggerFactory.getLogger(LogInterceptor.class);

    // ================================================================
    // Fields
    // ================================================================

    private static final ThreadLocal<String> START_TIMETHREAD_LOCAL =
            new NamedThreadLocal<>("ThreadLocal StartTime");

    // ================================================================
    // Constructors
    // ================================================================

    // ================================================================
    // Methods from/for super Interfaces or SuperClass
    // ================================================================

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String beginTime = DateUtils.getDateTime();
        START_TIMETHREAD_LOCAL.set(beginTime);

        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        if (modelAndView != null) {
            LOGGER.info("ViewName: " + modelAndView.getViewName());
        }
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

        // 得到线程绑定的局部变量（开始时间）
        String beginTime = START_TIMETHREAD_LOCAL.get();
        // 2、结束时间
        String endTime = DateUtils.getDateTime();

        // TODO 异步日志模块
    }

    // ================================================================
    // Public or Protected Methods
    // ================================================================


    // ================================================================
    // Getter & Setter
    // ================================================================

    // ================================================================
    // Private Methods
    // ================================================================


    // ================================================================
    // Inner or Anonymous Class
    // ================================================================

    // ================================================================
    // Test Methods
    // ================================================================

}
